package com.example.news;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.news.sql.MYSQL;

import org.json.JSONException;
import org.json.JSONObject;

public class LoginAvtivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tv_zhuce;
    private Toolbar log_toolbar;
    private EditText edit_yonghuming;
    private EditText edit_mima;
    private CheckBox box_mima;
    private CheckBox box_denglu;
    private Button btn_denglu;
    private TextView wangjimiam;
    String a;
    String b;
    String yonghuming=null;
    String mima=null;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_avtivity);
        initView();
        yonghuming = edit_yonghuming.getText().toString().trim();
        mima = edit_mima.getText().toString().trim();


    }

    private void initView() {
        tv_zhuce = (TextView) findViewById(R.id.tv_zhuce);
        log_toolbar = (Toolbar) findViewById(R.id.log_toolbar);
        setSupportActionBar(log_toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        log_toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        edit_yonghuming = (EditText) findViewById(R.id.edit_yonghuming);
        edit_mima = (EditText) findViewById(R.id.edit_mima);
        edit_yonghuming.setText(a);
        edit_mima.setText(b);

        edit_mima.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                edit_mima.clearFocus();
                edit_yonghuming.clearFocus();
                InputMethodManager inputMethodManager= (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
                View view=getWindow().peekDecorView();
                if (view!=null){
                    inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(),0);
                }
                return false;
            }
        });

        box_mima = (CheckBox) findViewById(R.id.box_mima);
        box_denglu = (CheckBox) findViewById(R.id.box_denglu);
        btn_denglu = (Button) findViewById(R.id.btn_denglu);
        tv_zhuce.setOnClickListener(this);
        btn_denglu.setOnClickListener(this);
        wangjimiam = (TextView) findViewById(R.id.wangjimiam);
        wangjimiam.setOnClickListener(this);

    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_denglu:
                String yonghuming = edit_yonghuming.getText().toString().trim();
                String mima = edit_mima.getText().toString().trim();
                submit();//判断输入是否合法
                JSONObject jsonObject=new JSONObject();
                try {
                    jsonObject.put("name",yonghuming);
                    jsonObject.put("password",mima);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                RequestQueue requestQueue= Volley.newRequestQueue(LoginAvtivity.this);
                JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.POST, "http://192.168.0.107:8080/IDeaJ/WorldCupApp/user/login", jsonObject.toString(), new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {

                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {

                    }
                });
                requestQueue.add(jsonObjectRequest);



                    MYSQL mysql = new MYSQL(LoginAvtivity.this);//创建数据库
                    SQLiteDatabase sqLiteDatabase = mysql.getReadableDatabase();
                    Cursor cursor = sqLiteDatabase.rawQuery("select * from information where name=? and password=?", new String[]{yonghuming, mima});
                    //将输入的内容与数据库进行比对,并获取数据结构集
                    if (cursor != null) {//判断该否有用户存在
                        String aa = null;
                        while (cursor.moveToNext()) {//取数据库中查询到的结果
                            aa = cursor.getString(cursor.getColumnIndex("name"));
                            Intent intent = new Intent(this, MainActivity.class);//页面跳转,将查询到的用户名回传到页面中
                            intent.putExtra("name", aa);
                            startActivity(intent);
                            finish();//关闭页面
                            Toast.makeText(LoginAvtivity.this, "欢迎" + aa + "登陆", Toast.LENGTH_SHORT).show();
                        }
                    } else  if(cursor==null){
                        Toast.makeText(LoginAvtivity.this, "账号或密码错误", Toast.LENGTH_SHORT).show();
                    }


                break;
            case R.id.tv_zhuce:
                finish();

                startActivity(new Intent(LoginAvtivity.this, RegisterActivity.class));
                break;
            case R.id.wangjimiam:
                startActivity(new Intent(LoginAvtivity.this,WangjimiamActivity.class));
                break;
        }
    }

    private void submit() {
        // validate
        String yonghuming = edit_yonghuming.getText().toString().trim();
        if (TextUtils.isEmpty(yonghuming)) {
            Toast.makeText(this, "用户名", Toast.LENGTH_SHORT).show();
            return;
        }

        String mima = edit_mima.getText().toString().trim();
        if (TextUtils.isEmpty(mima)) {
            Toast.makeText(this, "密码", Toast.LENGTH_SHORT).show();
            return;
        }

        // TODO validate success, do something


    }
}
